import matplotlib.pyplot as plt
import os 
import numpy as np
import pandas as pd
from PIL import Image

def plot_csv_data(
    file_path,
    x_range=None,
    y_label='Isc(uA)',
    x_label='Times(s)',
    title=None,
    line_color='blue',
    line_width=4,
    figsize=(13.6,10.4),
    dpi=100,
    sample_rate = 1000,
    x_ticks_interval=100,
    x_lim=None,
    y_margins=0.15,
    font_main={'family': 'Times New Roman', 'size':30,'weight': 'bold'},
    font_ticks={'family': 'Times New Roman', 'size':30,'weight': 'bold'},
    is_reversed = False,
    legend = None
):
    """
    绘制指定CSV文件中的数据图表。
    参数:
        file_path (str): CSV 文件路径。
        x_range (tuple): x轴范围 (start, stop)。如果为 None，根据数据自动生成。
        y_label (str): y轴标签。
        x_label (str): x轴标签。
        title (str): 图表标题。
        line_color (str): 折线颜色。
        line_width (float): 折线宽度。
        figsize (tuple): 图表大小。
        dpi (int): 图表分辨率。
        x_ticks_interval (int): x轴标签间隔。
        x_lim (tuple): x轴显示范围。
        y_margins (float): y轴边距。
        font_main (dict): 主标签字体。
        font_ticks (dict): 刻度字体。
    """
    # 读取CSV文件
    data = pd.read_csv(file_path)
    values = data['Value']
    if is_reversed:
        pass
        # values = [-i for i in values]
        # values.reverse()
    # 定义数据和x轴范围
    y_data = np.array(values)
    print("the y data lenth is: ")
    print(len(y_data))
    print("\n")
    if x_range is None:
        x_data = np.arange(len(y_data)) / sample_rate# 默认根据数据长度生成
    else:
        x_data = np.arange(*x_range) / sample_rate
    
    # 设置图表
  
    plt.figure(figsize=figsize,dpi=dpi)
    plt.plot(x_data, y_data, color=line_color, linewidth=line_width)

    # 设置坐标轴样式
    ax = plt.gca()
    for spine in ['bottom', 'left', 'right', 'top']:
        ax.spines[spine].set_color('black')
        ax.spines[spine].set_linewidth(2)

    # 设置标签和标题
    plt.xlabel(x_label, fontproperties=font_main)
    plt.ylabel(y_label, fontproperties=font_main)
    plt.title(title, fontproperties=font_main)
    
    # 设置刻度样式
    plt.xticks(fontproperties=font_ticks)
    plt.yticks(fontproperties=font_ticks)
    plt.xticks(x_data[::x_ticks_interval])

    legend_font = {'family': 'Times New Roman', 'size': 30, 'weight': 'bold'}
    plt.legend([legend],prop=legend_font,loc='upper right',frameon=False, facecolor='none')

    # 设置边界和显示范围
    plt.margins(y=y_margins)
    if x_lim:
        plt.xlim(x_lim)

    # 显示图表
    save_path = os.path.splitext(file_path)[0] + '.png'
    plt.savefig(save_path)
    plt.show()
    # Image.open(save_path).show()
    

if __name__ == '__main__':
    # 替换文件路径为您的实际CSV文件路径
    csv_path = []
    csv_path.append('./100hz_c.csv')
    csv_path.append('./100hz_q.csv')
    csv_path.append('./100hz_v.csv')
    csv_path.append('./10hz_c.csv')
    csv_path.append('./10hz_q.csv')
    csv_path.append('./10hz_v.csv')
    csv_path.append('./1hz_c.csv')
    csv_path.append('./1hz_q.csv')
    csv_path.append('./1hz_v.csv')
    plot_csv_data(
        file_path=csv_path[0],
        y_label='Isc(uA)',
        x_label='Times(s)',
        line_color='green',
        x_lim=(0,0.09),
        sample_rate=10000,
        is_reversed=True,
        legend = 'Proposed Circuit'
    )
    plot_csv_data(
        file_path=csv_path[1],
        y_label='Qsc(nC)',
        x_label='Times(s)',
        title= None,
        line_color='green',
        x_lim=(0,0.09),
        sample_rate=10000,
        legend = 'Proposed Circuit'
    )
    plot_csv_data(
        file_path=csv_path[2],
        y_label='Voc(V)',
        x_label='Times(s)',
        title= None,
        line_color='green',
        sample_rate=5000,
        x_lim=(0,0.09),
        x_ticks_interval=50,
        legend = 'Proposed Circuit',
        )
    plot_csv_data(
        file_path=csv_path[3],
        y_label='Isc(uA)',
        x_label='Times(s)',
        title= None,
        line_color='purple',
        x_lim=(0,0.9),
        is_reversed=True,
        legend = 'Proposed Circuit',
    )
    plot_csv_data(
        file_path=csv_path[4],
        y_label='Qsc(nC)',
        x_label='Times(s)',
        title= None,
        line_color='purple',
        x_lim=(0,0.9),
        legend = 'Proposed Circuit',
    )
    plot_csv_data(
        file_path=csv_path[5],
        y_label='Voc(V)',
        x_label='Times(s)',
        title= None,
        line_color='purple',
        x_lim=(0,0.9),
        legend = 'Proposed Circuit',
    )
    plot_csv_data(
        file_path=csv_path[6],
        y_label='Isc(uA)',
        x_label='Times(s)',
        title= None,
        line_color='blue',
        x_ticks_interval=1000,
        x_lim=(0,8.3),
        is_reversed=True,
        legend = 'Proposed Circuit',
    )
    plot_csv_data(
        file_path=csv_path[7],
        y_label='Qsc(nC)',
        x_label='Times(s)',
        title= None,
        line_color='blue',
        x_ticks_interval=1000,
        x_lim=(0,8.3),
        legend = 'Proposed Circuit',
    )
    plot_csv_data(
        file_path=csv_path[8],
        y_label='Voc(V)',
        x_label='Times(s)',
        title= None,
        line_color='blue',
        x_ticks_interval=1000,
        x_lim=(0,8.3),
        legend = 'Proposed Circuit',
    )
